#include "TouchCore.h"
#include "VGPLib.h"
#include "GameInput.h"
#include "Engine.h"

short TouchCore::s_MenuSelection;
short TouchCore::s_MenuSelectionReleased;
TouchCore* TouchCore::touchCore;

TouchCore::TouchCore()
{
	iNumOfOption = 0;	
	isTouchReleased = false;
	iCurrentHoverButton = -1;
	touchCore = NULL;
}

TouchCore::~TouchCore()
{
}

TouchCore* TouchCore::GetTouch()
{
	if ( touchCore == NULL)
		touchCore = new TouchCore();
	return touchCore;
}

bool TouchCore::IsTouchReleased()
{
	return isTouchReleased;
}
	
//Get current select option
int TouchCore::GetCurrentMenuItemID(int index)
{
	return index;
}

//Reset menu when change state
void TouchCore::MenuReset()
{
	iNumOfOption = 0;	
	iCurrentHoverButton = -1;
}

//add option in menu
void TouchCore::MenuAddOption(int x, int y, int w, int h, int anchor)
{
	if (iNumOfOption < MAX_TOUCH_OPTION)
	{
		m_Menu[iNumOfOption].x = x;
		m_Menu[iNumOfOption].y = y;
		m_Menu[iNumOfOption].w = w;
		m_Menu[iNumOfOption].h = h;
		m_Menu[iNumOfOption].anchor = anchor;
		iNumOfOption++;
	}
}

void TouchCore::MenuAddOption( int x, int y, int w, int h )
{
	MenuAddOption( x, y, w, h, VGPLib::TOP | VGPLib::LEFT );
}
//draw to debug touch
void TouchCore::DebugTouch()
{
	if (iNumOfOption > 0)
		for (int i = 0; i < iNumOfOption; i++)
			VGPLib::GetVGPLib()->PaintRectangle(m_Menu[i].x, m_Menu[i].y, m_Menu[i].w, m_Menu[i].h, m_Menu[i].anchor);
		
}

//update menu and return current select option (index)
void TouchCore::MenuUpdate(int x, int y, int state)
{
	if (state == POINTER_NONE)
	{
		iCurrentHoverButton = -1;
		return;
	}
	int index = -1;
	for (int i = 0; i < iNumOfOption; i++)
	{		
		if (IsTouchRectangle(x, y, m_Menu[i].x, m_Menu[i].y, m_Menu[i].w, m_Menu[i].h, m_Menu[i].anchor))
		{
			index = i;
			break;
		}
	}
	if (index >= 0)
	{
		switch (state)
		{
			case POINTER_DOWN: //pressed
			case POINTER_DRAGGED: //dragged
				iCurrentHoverButton = index;
				OnFocusOption(index);
				isTouchReleased = false;
				//LOGI("\nHover option: %d" + index);
				break;				
			case POINTER_UP: //released
				if (iCurrentHoverButton == index)
				{
					GameInput *gameInput = GameInput::GetGameInput();
					if (IsTouchRectangle(gameInput->pointerX, gameInput->pointerY, m_Menu[index].x, m_Menu[index].y, m_Menu[index].w, m_Menu[index].h, m_Menu[index].anchor))
					{
						OnReleaseOption(index);
						isTouchReleased = true;
						//LOGI("\nSelect option: %d" + index);
					}
				}
				else
				{
					GameInput *gameInput = GameInput::GetGameInput();
					gameInput->ClearState();
				}
				break;
		}
	}
	else
	{
		if (iCurrentHoverButton >= 0)
		{
			iCurrentHoverButton = index;
			OnLostFocusOption();
		}		
	}
}

void TouchCore::MenuUpdate()
{
	GameInput *gameInput = GameInput::GetGameInput();
	MenuUpdate(gameInput->pointerX, gameInput->pointerY, gameInput->pointerState);
}

//Touch on rectangle
bool TouchCore::IsTouchRectangle(int pointerX, int pointerY, int x, int y, int w, int h, int flag)
{
	if (flag == ( VGPLib::TOP | VGPLib::RIGHT ) )
	{
		x = x - w;
	}
	else if (flag == ( VGPLib::TOP | VGPLib::CENTER ) )
	{
		x = x - w/2;
	}
	else if (flag == ( VGPLib::LEFT | VGPLib::BOTTOM ) )
	{
		y = y - h;
	}
	else if (flag == ( VGPLib::RIGHT | VGPLib::BOTTOM ) )
	{
		x = x - w;
		y = y - h;
	}
	else if (flag == ( VGPLib::CENTER | VGPLib::BOTTOM ) )  
	{
		x = x - w/2;
		y = y - h;
	}
	else if (flag == ( VGPLib::CENTER | VGPLib::CENTER ) )
	{
		x = x - w/2;
		y = y - h/2;
	}
	return	(pointerX > x && pointerX < x + w && pointerY > y && pointerY < y + h);	
}

bool TouchCore::IsTouchRectangle(int pointerX, int pointerY, int x, int y, int w, int h)
{
	return IsTouchRectangle(pointerX, pointerY, x, y, w, h, VGPLib::TOP | VGPLib::LEFT);
}

//Touch on circle (x, y, radius)
bool TouchCore::IsTouchCircle(int pointerX, int pointerY, int x, int y, int radius)
{
	//distance from x, y to pointerX, pointerY
	int d = (pointerX - x)*(pointerX - x) + (pointerY - y)*(pointerY - y); 
	return d <= radius*radius;
}

int TouchCore::GetAngles(int pointerX, int pointerY, int x, int y)
{
	float d = sqrt(1.0*(pointerX - x)*(pointerX - x) + (pointerY - y)*(pointerY - y));	
	float alpha = acos((pointerX - x)/d)*180.0/3.14;

	if (pointerY > y)
		alpha = 360 - alpha;

	return (int)alpha;
}

//Focus to select
void TouchCore::OnFocusOption(int index)
{
	//todo:  your code here
	///LOGI("\n Focus index: %d", index);
	s_MenuSelection = index;

}

//Lost focus option
void TouchCore::OnLostFocusOption()
{
	//todo:  your code here
	s_MenuSelection = -1;
	s_MenuSelectionReleased = -1;
}

//Release select option
void TouchCore::OnReleaseOption(int index)
{
	//todo:  your code here
	//LOGI("\n Release index: %d", index);
	s_MenuSelectionReleased = index;
}